using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestMonkey.Core.Templates
{
    public class Helper : TemplateProcessor
    {
        public Helper()
        {
            CreateMode = CreateMode.CreateIfMissing;
        }

        public Helper(CreateMode createMode)
        {
            CreateMode = createMode;
        }

        public override bool ShouldRun(Type type)
        {
            return !type.IsEnum && type.HasConstructors();
        }

        protected override void OnProcessTemplate(Type type, StringBuilder template)
        {

			if (!type.IsCreatable())
				template.Replace("$CreateObject$", "\t\t\tAssert.Inconclusive(\"A custom version of CreateObject is needed.\");" + Environment.NewLine + "\t\t\treturn default($ClassName$);");
			else if (type.HasDefaultConstructor())
				template.Replace("$CreateObject$", "\t\t\treturn new " + type.Name + "();");
			else
				template.Replace("$CreateObject$", "\t\t\treturn " + type.GetConstructorCode() + ";");

			if (type.GetInterfaces().Any(i => i.Name == "IComparable"))
			{
				var part = LoadTemplate("Helper_Ordered", false);
				template.Replace("$Helper_Ordered$", part.ToString());
			}
			else
				template.Replace("$Helper_Ordered$", "");

			base.OnProcessTemplate(type, template);
        }

    }
}
